package com.baosight.sczx.zm.cc.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.zm.cc.domain.ZMCCQualitystandardClassify;

/**
 * 质量标准化设置 分类
 * <p>Title: ServiceZMCC01.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 严洪浩
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-28 上午01:53:07  严洪浩  Create
 *</pre>
 */
public class ServiceZMCC02 extends ServiceEPBase {
	
	/**
	 * 初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS41</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-3上午05:31:26 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCC01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-28上午02:33:54 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdParentId = inInfo.getString("fdParentId");//管理分项ID
		StaticTools.setQueryParameters(inInfo, "fdParentId", fdParentId, true);//添加查询条件
		
		EiInfo outInfo = super.query(inInfo, "ZMCCQualitystandardClassify.query", new ZMCCQualitystandardClassify());
		
		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCC01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-28上午02:33:48 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		//校验名称、简称是否重复
		boolean validate = this.validate(inInfo, "ZMCCQualitystandardClassify.validate", new HashMap<String, String>());
		
		if(validate) {
			return inInfo;
		}
		else {
			inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_PL_QUALITYSTANDARDCLASSIFY"));//主键
			inInfo.getBlock("result").setCell(0, "fdStatus", "在用");
			EiInfo outinfo = super.insert(inInfo, "ZMCCQualitystandardClassify.insert", "result");
			return outinfo;
		}
	}
	
	/**
	 * 
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCC01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-28上午02:33:40 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		//校验名称、简称是否重复
		Map<String, String> params = new HashMap<String, String>();
		params.put("fdKeyId", inInfo.getBlock("result").getCellStr(0, "fdKeyId"));
		
		boolean validate = validate(inInfo, "ZMCCQualitystandardClassify.validate", params);
		
		if(validate) {
			return inInfo;
		}
		else {
			EiInfo outInfo = super.update(inInfo, "ZMCCQualitystandardClassify.update");
			return outInfo;
		}
	}

	/**
	 * 
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCC01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-28上午02:33:32 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		 //删除质量标准化分类明细
		List<String> _list = new ArrayList<String>();
		Map<String,List<String>> params = new HashMap<String,List<String>>();

		for(int i = 0; i < inInfo.getBlock(EiConstant.resultBlock).getRowCount(); i++){
			_list.add(inInfo.getBlock("result").getCellStr(i, "fdKeyId"));
		}
		
		params.put("fdKeyIds", _list);
		
		boolean validate = this.canDelete(inInfo, "ZMCCQualitystandardClassify.validateHasChild", params);
		
		if(validate) {
			return inInfo;
		}
		else {
			EiInfo _temp = super.delete(inInfo, "ZMCCQualitystandardClassify.delete");
			EiInfo outInfo = this.query(inInfo);
		    outInfo.setMsg(_temp.getMsg());
		    outInfo.setDetailMsg(_temp.getDetailMsg());

			return outInfo;
		}
	}
	
	/**
	 * 编号名称重复校验
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-06-28下午04:18:21 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		String fdSerialnum = inInfo.getBlock("result").getCellStr(0, "fdSerialnum");
		String fdClassifyName = inInfo.getBlock("result").getCellStr(0, "fdClassifyName");
		params.put("fdSerialnum", fdSerialnum);
		params.put("fdClassifyName", fdClassifyName);
		
		List result = dao.query("ZMCCQualitystandardClassify.validate", params);
		if(result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			ZMCCQualitystandardClassify _temp = (ZMCCQualitystandardClassify)result.get(0);
			
			StringBuffer msg = new StringBuffer("提交失败，");
			if(_temp.getFdSerialnum() != null && _temp.getFdSerialnum().toString().equals(fdSerialnum)) {
				msg.append(" 序号 ");
			}
			if(_temp.getFdClassifyName() != null && _temp.getFdClassifyName().equals(fdClassifyName)) {
				msg.append(" 名称 ");
			}
			msg.append("重复！");
			
			inInfo.setMsg(msg.toString());
			return true;
		}
		return false;
	}
	
	/**
	 * 判断分类下面是否存在项目
	 * <p>Function: canDelete</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-8-17上午04:59:59 严洪浩
	 * </p>
	 *
	 */
	private boolean canDelete(EiInfo inInfo, String sqlName, Map<String,List<String>> params) {
		if(params == null) {
			params = new HashMap<String,List<String>>();
		}
		
		List<?> _list = dao.query(sqlName, params);
		if(_list.size() > 0) {
			inInfo.setStatus(-1);
			inInfo.setMsg("选择的分类下面存在项目，不能删除！");
			return true;
		}
		return false;
	}
}
